//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.client;

import java.util.HashSet;
import java.util.Set;

/**
 * 文字列のSanitize処理(HTMLのエスケープ処理)を行う
 * 
 * @author hisayori
 * 
 */
public class Sanitize {
	private static Sanitize instance = new Sanitize();

	/**
	 * インスタンスを取得する
	 * 
	 * @return インスタンス
	 */
	public static Sanitize getInstance() {
		return instance;
	}

	private static final String URL_LETTER = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ;/?:@&=+$,-_.!~*'()%";

	private final Set<Character> urlLetters = new HashSet<Character>();

	private Sanitize() {
		for (int i = 0; i < URL_LETTER.length(); ++i) {
			urlLetters.add(URL_LETTER.charAt(i));
		}
	}

	private static final String SUFFIX_LETTER[] = { "\r", "\n", " ", "　", "\t" };

	/**
	 * HTML文字列にSanitize処理を施す
	 * 
	 * @param s
	 *            処理する文字列
	 * @return Sanitize処理済の文字列
	 */
	public String html(String s) {
		String res = s;
		res = res.replaceAll("&", "&amp;");
		res = res.replaceAll("<", "&lt;");
		res = res.replaceAll(">", "&gt;");

		res = res.replaceAll("\r\n", "\n");
		// 空行は連続3行まで
		while (res.indexOf("\n\n\n\n\n") != -1) {
			res = res.replaceAll("\n\n\n\n\n", "\n\n\n\n");
		}

		// 最後の空白はカット
		boolean ok;
		do {
			ok = true;
			for (int i = 0; i < SUFFIX_LETTER.length; ++i) {
				if (res.endsWith(SUFFIX_LETTER[i])) {
					ok = false;
					res = res.substring(0, res.length() - 1);
				}
			}
		} while (!ok);

		res = res.replaceAll("\n", "<br/>");

		return res;
	}

	public String htmlChoice(String s) {
		s = html(s);
		return s.replaceAll("%n", "<br/>");
	}

	/**
	 * URIにSanitize処理を施す
	 * 
	 * @param s
	 *            処理する文字列
	 * @return Sanitize処理済の文字列(不適切な文字が含まれていた場合は空文字列を返す)
	 */
	public String uri(String s) {
		for (int i = 0; i < s.length(); ++i) {
			if (!urlLetters.contains(s.charAt(i))) {
				return "";
			}
		}

		String res = s;
		res = res.replaceAll("&", "&amp;");
		res = res.replaceAll("'", "&#39");

		return res;
	}
}
